<?php
    //Entry point
    
    
    date_default_timezone_set("Europe/London");
    define('PROCESS',true);
    
    define('PODCAST_TYPE', 1);
    define('VIDEO_TYPE', 2);
    define('NEWS_TYPE', 3);
    
    $start = microtime(TRUE);

    include_once("Conf/conf.conf.php");
    Config::inst("Conf/config.cfg");
    include_once("lib/list.inc.php");
    
    //Critical exception handler;
    
    $url = $_SERVER['REQUEST_URI'];
    
    $c = 0;
    $VM = ViewManager::inst();
    //$DOM = DataObjectManager::inst();
    $CMM = ControllerManager::inst();
    $args = decode_mrw_url($url); //Obtain url params - [0] and [1] are the controller info, [2] onwards are parameters
    $params = (count($args) > 2) ? array_slice($args,2) : null; //Additional params are only set if they exist;

    if (count($args) == 1 && $args[0] == false) {
        $CMM->registerControllerIntoChain('home', 'main');
    }
    elseif (count($args) < 2 || !$CMM->registerControllerIntoChain($args[0],$args[1],$params)) {
        $CMM->registerControllerIntoChain('system', 'notFound'); //Set controller to 404;
    }
    
    //Begin the main controller loop;
    try {
        $CMM->executeControllers();
    }
    catch (Exception $e) {
        //Any exceptions thrown this far are critical and will have ended the main loop prematurely, 
        //so we'll assemble an error message;
        //06/09 - This is a debug message. On production replace with a simple 'ohshi' screen.
        $message = <<<MSG
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>A critical exception has occurred!</title>
        
<style type="text/css">
body {
    font: 12pt Arial, Verdana, Calibri, sans-serif;
    background: #000000;
}

div.exception {
    background: #ffffff;
    position: absolute;
    top: 100px;
    left: 200px;
    right: 200px;
    
    margin: 0px;
    padding: 0px;
    
    border: 4px solid #973535;
}

h1 {
    margin: 0px;
    padding: 4px;
    background: #d40000;
    color: #ffffff;
    
    font-size: 28pt;
}

h2 {
    background: #f06262;
    margin: 0px;
    padding: 4px;
    color: #ffffff;
    
    font-size: 14pt;
}

p, pre {
    padding: 8px;
}

pre {
    display: block;
    overflow: auto;
    font-size: 8pt;
}

</style>
</head>
<body>
<div class="exception">
<h1>DON'T PANIC!</h1>
<h2>A Critical Exception has occurred.</h2>
<p><b>Don't panic</b> - this page has come up because something went a little bit wrong, 
and this page gives the information to help figure out what happened.</p>
<p>First, click the back button on your web browser, and try and do what you just did again - if this error
page comes up again, please copy and paste the information below into an email and send it to the system admin.</p>
<p>This is what appears to have gone wrong: <em>{$e->GetMessage()}</em>, and what follows is some technical
details on the error.</p>
<pre>
<b>Code:</b> {$e->GetCode()}
<b>File:</b> {$e->GetFile()}
<b>Line:</b> {$e->GetLine()}
<b>Stack Trace:</b>
{$e->GetTraceAsString()}
</pre>
<p>Sorry!</p>
</div>
MSG;
        die ($message);
    }

    $VM->registerStyle("defaultstyle");
    $VM->registerStyle("nav");
    $VM->assemblePage();
    echo ($VM->pageData);
        
    $tend = microtime(TRUE);
    $exetime = $tend - $start;
    echo ("\n<!-- Time Taken to render page: $exetime seconds -->");
?>